﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.IO;
using System.Web.SessionState;
namespace stock.GuPiao
{
    /// <summary>
    /// CodeImage 的摘要说明
    /// </summary>
    public class CodeImage : IHttpHandler, IRequiresSessionState
    {
        Random ran;
        public void ProcessRequest(HttpContext context)
        {
            ran = new Random();
            String str = getRandomValidate(4);
            HttpContext.Current.Session["check"] = str;
            getImageValidate(str);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private String getRandomValidate(int len)
        {
            Random rd = new Random();
            string[] source = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
            String code = "";
            for (int i = 0; i < len; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
        private void getImageValidate(String strValue)
        {
            int width = Convert.ToInt32(strValue.Length * 13);
            Bitmap img = new Bitmap(width, 22);
            Graphics gfc = Graphics.FromImage(img);
            gfc.Clear(Color.White);
            drawLine(gfc, img);
            Font font = new Font("arial", 12, FontStyle.Bold);
            System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, img.Width, img.Height), Color.DarkOrchid, Color.Blue, 1.5f, true);
            gfc.DrawString(strValue, font, brush, 3, 2);
            drawPoint(img);
            gfc.DrawRectangle(new Pen(Color.DarkBlue), 0, 0, img.Width - 1, img.Height - 1);
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ContentType = "image/gif";
            HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            gfc.Dispose();
            img.Dispose();
            HttpContext.Current.Response.End();
        }

        private void drawPoint(Bitmap img)
        {
            int col = ran.Next();
            for (int i = 0; i < 100; i++)
            {
                int x = ran.Next(img.Width);
                int y = ran.Next(img.Height);
                img.SetPixel(x, y, Color.FromArgb(col));
            }
        }

        private void drawLine(Graphics gfc, Bitmap img)
        {
            for (int i = 0; i < 8; i++)
            {
                int x1 = ran.Next(img.Width);
                int y1 = ran.Next(img.Height);
                int x2 = ran.Next(img.Width);
                int y2 = ran.Next(img.Height);
                gfc.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
            }
        }
    }
}